{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1.输入某年某月某日，判断这一天是这一年的第几天？\n",
    "def day_of_year():\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    if (year % 4 == 0 and year % 100!= 0) or year % 400 == 0:\n",
    "        days_in_month[2] = 29\n",
    "    total_days = sum(days_in_month[:month]) + day\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{total_days}天\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#2.判断1000中有多少个素数，并输出所有素数。\n",
    "def count_prime_numbers():\n",
    "    prime_numbers = []\n",
    "    for num in range(2, 1001):\n",
    "        is_prime = True\n",
    "        for i in range(2, int(num ** 0.5) + 1):\n",
    "            if num % i == 0:\n",
    "                is_prime = False\n",
    "                break\n",
    "        if is_prime:\n",
    "            prime_numbers.append(num)\n",
    "    print(f\"1000以内的素数有{len(prime_numbers)}个，分别是{prime_numbers}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#3.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。\n",
    "def grade_judgment():\n",
    "    score = float(input(\"请输入成绩: \"))\n",
    "    grade = \"A\" if score >= 90 else \"B\" if score >= 60 else \"C\"\n",
    "    print(f\"该成绩对应的等级是{grade}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n",
    "def count_characters():\n",
    "    input_str = input(\"请输入一行字符串: \")\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    for char in input_str:\n",
    "        if char.isalpha():\n",
    "            letters += 1\n",
    "        elif char.isspace():\n",
    "            spaces += 1\n",
    "        elif char.isdigit():\n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "    print(f\"英文字母个数: {letters}，空格个数: {spaces}，数字个数: {digits}，其它字符个数: {others}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#5.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n",
    "def find_perfect_numbers():\n",
    "    perfect_numbers = []\n",
    "    for num in range(1, 1001):\n",
    "        factors = []\n",
    "        for i in range(1, num):\n",
    "            if num % i == 0:\n",
    "                factors.append(i)\n",
    "        if sum(factors) == num:\n",
    "            perfect_numbers.append(num)\n",
    "    print(f\"1000以内的完数有{perfect_numbers}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#6.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n",
    "def factorial_sum():\n",
    "    n = int(input(\"请输入N: \"))\n",
    "    sum_value = 0\n",
    "    factorial = 1\n",
    "    for i in range(1, n + 1):\n",
    "        factorial *= i\n",
    "        sum_value += factorial\n",
    "    print(f\"1+2!+3!+...+{n}!的值为{sum_value}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#7.输入任意奇数N，按如下要求打印出图案（菱形）    *  *** ************（N控制行数和本行的星数） *****  ***   *\n",
    "def count_prime_numbers():\n",
    "    prime_numbers = []\n",
    "    for num in range(2, 1001):\n",
    "        is_prime = True\n",
    "        for i in range(2, int(num ** 0.5) + 1):\n",
    "            if num % i == 0:\n",
    "                is_prime = False\n",
    "                break\n",
    "        if is_prime:\n",
    "            prime_numbers.append(num)\n",
    "    print(f\"1000以内的素数有{len(prime_numbers)}个，分别是{prime_numbers}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#8.对10个数进行排序\n",
    "def sort_ten_numbers():\n",
    "    numbers = []\n",
    "    for _ in range(10):\n",
    "        num = int(input(\"请输入一个数: \"))\n",
    "        numbers.append(num)\n",
    "    numbers.sort()\n",
    "    print(f\"排序后的数组为{numbers}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#9.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n",
    "def insert_into_sorted_array():\n",
    "    sorted_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]  \n",
    "    num = int(input(\"请输入要插入的数: \"))\n",
    "    inserted = False\n",
    "    for index in range(len(sorted_array)):\n",
    "        if num < sorted_array[index]:\n",
    "            sorted_array.insert(index, num)\n",
    "            inserted = True\n",
    "            break\n",
    "    if not inserted:\n",
    "        sorted_array.append(num)\n",
    "    print(f\"插入后的数组为{sorted_array}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#10.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n",
    "def last_remaining_person():\n",
    "    n = int(input(\"请输入人数n: \"))\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    print(f\"最后留下的是原来第{people[0]}号的那位\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第1号的那位\n"
     ]
    }
   ],
   "source": [
    "choice = input(\"请输入要运行的程序编号(1-10): \")\n",
    "if choice == \"1\":\n",
    "    day_of_year()\n",
    "elif choice == \"2\":\n",
    "    count_prime_numbers()\n",
    "elif choice == \"3\":\n",
    "    grade_judgment()\n",
    "elif choice == \"4\":\n",
    "    count_characters()\n",
    "elif choice == \"5\":\n",
    "    find_perfect_numbers()\n",
    "elif choice == \"6\":\n",
    "    factorial_sum()\n",
    "elif choice == \"7\":\n",
    "    print_diamond()\n",
    "elif choice == \"8\":\n",
    "    sort_ten_numbers()\n",
    "elif choice == \"9\":\n",
    "    insert_into_sorted_array()\n",
    "elif choice == \"10\":\n",
    "    last_remaining_person()\n",
    "else:\n",
    "    print(\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
